from Python_Structure_Algorithm.Python_hash_15_copy import HashTable


class SetADT(HashTable):
    def add(self, key):
        super().add(key, True)

    def __and__(self, other_set):  # a & b
        new_set = SetADT()
        for element_a in self:
            if element_a in other_set:
                new_set.add(element_a)
        return new_set

    def __or__(self, other_set):  # a | b
        new_set = SetADT()
        for element_a in self:
            new_set.add(element_a)
        for element_b in other_set:
            new_set.add(element_b)
        return new_set

    def __sub__(self, other_set):  # a - b
        new_set = SetADT()
        for element_a in self:
            if element_a not in other_set:
                new_set.add(element_a)
        return new_set

    def __xor__(self, other_set):  # a ^ b 即 a | b - a & b
        new_set = SetADT()
        for element_a in self:
            if element_a not in other_set:
                new_set.add(element_a)
        for element_b in other_set:
            if element_b not in self:
                new_set.add(element_b)
        return new_set

    def pop(self):
        pass



def test_setadt():
    seta = SetADT()
    seta.add(1)
    seta.add(2)
    seta.add(3)

    setb = SetADT()
    setb.add(3)
    setb.add(4)
    setb.add(5)

    assert sorted(list(seta & setb)) == [3]
    assert sorted(list(seta | setb)) == [1, 2, 3, 4, 5]
    assert sorted(list(seta - setb)) == [1, 2]
    assert sorted(list(seta ^ setb)) == [1, 2, 4, 5]
    setb.remove(5)
    assert sorted(list(setb)) == [3, 4]
